<?php

namespace Home\Controller;

use Think\Controller;
use Home\Controller\PayApiController;
// 指定允许其他域名访问  
header('Access-Control-Allow-Origin:*');  
// 响应类型  
header('Access-Control-Allow-Methods:*');  
// 响应头设置  
header('Access-Control-Allow-Headers:x-requested-with,content-type');

class DriverOrderController extends PayApiController{

    /**
     * @func 司机二维码付款页面
     * @param price 价格
     * @param orderid 订单id
     */
    public function endarriveCCBPay(){
        if(!IS_POST){
            datamsg(0,'非法操作');
        }
        $user_id=I("post.user_id");
        $token=I("post.token");
        user_cinfo($user_id,$token);
        $id = I("post.id");
        if(empty($id)){
            datamsg(0,'订单id不能为空');
        }
        $xllogarr = D('xllog')->find($id);
        if($xllogarr){
            if($xllogarr['status'] == 6){
                datamsg(0,'该订单已经支付');
            }else{
                parent::CCBPay($xllogarr['price']*$xllogarr['num'],$xllogarr['orderid']);
            }
        }else{
            datamsg(0,'没有查询到该订单');
        }
    }


    /**
     * @func 订单查询接口
     * @param price 价格
     * @param orderid 订单id
     */
    public function queryCCBPay(){
        self::queryCCBPayorder();
    }



    /**
     * @func 异步通知接口
     * @param price 价格
     * @param orderid 订单id
     */
    public function returnMessage(){
        $returnmsg = self::returnapi();
        //file_put_contents("getlog1.txt",json_encode($returnmsg));
        if($returnmsg['SUCCESS'] == 'Y') {

            $xllogarr = D('xllog')->where(array('orderid'=>$returnmsg['ORDERID']))->find(); //获取当前的订单信息
            $user_arr = D('wxuser')->where(array("openid"=>$xllogarr['openid']))->find();//获取用户的基本信息

            if($xllogarr['pay_statu']!=1 && !empty($xllogarr['openid'])){
                $yue=$user_arr['yue']+$returnmsg['PAYMENT'];
                $result=D('wxuser')->where(array("openid"=>$xllogarr['openid']))->save(array('yue'=>$yue));
            }

            if($xllogarr['openid']==''){
                $xllog = D('xllog')->where(array('orderid' => $returnmsg['ORDERID']))->save(array('pay_statu'=>1,'pay_status_sk' => 0, 'pay_status_sk' => 0,'newpay'=>1, 'total_price' => $returnmsg['PAYMENT'],'siji'=>time()));//修改数据库记录
            }else{
                $xllog = D('xllog')->where(array('orderid' => $returnmsg['ORDERID']))->save(array('status' => 6,'pay_statu'=>1,'pay_status_sk' => 0, 'pay_status_sk' => 0,'total_price' => $returnmsg['PAYMENT'],'siji'=>time()));//修改数据库记录
            }
            $xllogarr = D('xllog')->where(array('orderid'=>$returnmsg['ORDERID']))->find(); //获取当前的订单信息

            $user_arr = D('wxuser')->where(array("openid"=>$xllogarr['openid']))->find();//获取用户的基本信息

            if($xllog){
                //file_put_contents("getlog5.txt",json_encode($result));
//                file_put_contents(date('Y-m-d H:i:s',time()).'.log',json_encode(222));
//                file_put_contents("getlog6.txt",'写入用户余额失败');
            }else{
                file_put_contents("getlog7.txt",'异步修改数据库失败');
            }
        }else{
            file_put_contents("getlog8.txt",json_encode($returnmsg));
        }
    }


    /**
     * @func 支付成功轮训跳转页面
     * @param user_id 当前登录的id
     * @param token 登录时候的token
     * @return json string
     */
    public function returnredict(){
        $user_id=I("post.user_id");
        $token=I("post.token");
        user_cinfo($user_id,$token);
        $id = I("post.id");
        if(empty($id)){
            datamsg(0,'订单id不能为空');
        }
        if(empty($id)){
            datamsg(0,'订单ID不能为空');
        }
        $xllogarr = D('xllog')->find($id);
        if($xllogarr){
            if($xllogarr['status'] == 6){
                datamsg(1,'支付成功');
            }else{
                datamsg(0,'等待支付');
            }
        }else{
            datamsg(0,'没有找到订单号');
        }
    }

    /**
     * 订单完成
     * @param user_id 用户id
     * @param token 用户token
     * @param order_id 用户订单id
     */
    public function endNewpay(){
        if(!IS_POST){
            datamsg(0,'非法操作');
        }
        $user_id    = I("user_id");
        $token      = I("token");
        $user       = user_cinfo($user_id,$token);
        $order_id   = I("order_id");


        empty($order_id) && datamsg(0,'找不到该订单');
        $order      = D('xllog')->where(array('id'=>$order_id,'openid'=>$user['openid']))->find();

        empty($order) && datamsg(0,'找不到该订单');

        if($order['status']==7){
            datamsg(0,'订单已撤销');
        }elseif ($order['status']==6){
            datamsg(0,'订单已完成');
        }
        $price      = $order['price']*$order['num'];
        $res = D('wxuser')->where(array('uid'=>$user_id))->setInc('yue',$price);

        $arr    = array(
            'status'    => 6,
            'pay_statu' => 1,
            'pay_status_sk' => 0,
        );
        $res = D('xllog')->where(array('id'=>$order_id))->save($arr);
        empty($res) && datamsg(0,'失败');
        datamsg(1,'成功');
    }

    /**
     * @func 现金支付 无需写入到用户的余额表里面
     */
    public function cashmoney(){
        $user_id=I("post.user_id");
        $token=I("post.token");
        user_cinfo($user_id,$token);
        $id=I("post.id");
        if(empty($id)){
            datamsg(0,'订单id不能为空');
        }
        $xllog=D("xllog")->find($id);
        if($xllog) {
            $okzhifu = D('xllog')->where(array('id' => $id))->save(array('total_price' => $xllog['price'] * $xllog['num'],'pay_statu'=>1, 'pay_status_sk' => 1, 'status' => 6, 'siji' => time()));
            if ($okzhifu) {
                datamsg(1, '收钱成功');
            } else {
                datamsg(0, '系统错误');
            }
        }else{
            datamsg(0,'没有找到订单');
        }
    }


    public function dreiverorder(){
        $user_id=I("post.user_id");
        $token=I("post.token");
        user_cinfo($user_id,$token);
        $id=I("post.id");
        $orderid = M('xllog')->where(array('id'=>$id))->find();
        $data['all_price'] = $orderid['price'] * $orderid['num'];
        $data['id'] = $orderid['id'];
        $data['all_price'] = sprintf("%.2f",$data['all_price']);
        if($orderid){
            datamsg(1,'获取成功',$data);
        }else{
            datamsg(0,'获取失败');
        }
    }


}